// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Extensions;
    using Filters;

    public partial class FilterPickerGroup : UserControl
    {
        private ICollection<string> _fieldsForPickers;

        private bool _removeDuplicatesOnGetFilters;

        public FilterPickerGroup()
        {
            InitializeComponent();
            _removeDuplicatesOnGetFilters = true;
        }

        /// <summary>
        /// Default is true.  If true, on calling GetFilters, it will remove duplicate items.
        /// </summary>
        public bool RemoveDuplicatesOnGetFilters
        {
            get { return _removeDuplicatesOnGetFilters; }
            set { _removeDuplicatesOnGetFilters = value; }
        }

        public bool HasFields
        {
            get { return (_fieldsForPickers != null) && (_fieldsForPickers.Count > 0); }
        }

        public IList<SearchFilter> Filters
        {
            get
            {
                List<SearchFilter> filters = new List<SearchFilter>();
                foreach (FilterPicker p in panel.Controls)
                {
                    if (null == p)
                    {
                        continue;
                    }

                    SearchFilter sf = new SearchFilter
                                          {
                                              Field = p.Field,
                                              SearchOperator = p.SearchOperator,
                                              SearchText = p.SearchText
                                          };

                    if (RemoveDuplicatesOnGetFilters)
                    {
                        if ((!filters.Contains(sf)))
                        {
                            filters.Add(sf);
                        }
                    }
                    else
                    {
                        filters.Add(sf);
                    }
                }
                return filters;
            }
        }

        public void AddFilter()
        {
            FilterPicker p = new FilterPicker();
            p.SetFields(_fieldsForPickers);
            panel.Controls.Add(p);
        }

        public void AddFilterPicker(FilterPicker picker)
        {
            panel.Controls.Add(picker);
        }

        public void RemoveFilterPicker(FilterPicker picker)
        {
            if (!panel.Controls.Contains(picker))
            {
                return;
            }

            panel.Controls.Remove(picker);
            panel.Invalidate();
        }

        /// <summary>
        /// Removes all FilterPickers from the group
        /// </summary>
        public void Clear()
        {
            panel.Controls.Clear();
        }


        public void SetFieldsForAllPickers(ICollection<string> fields)
        {
            _fieldsForPickers = fields;

            foreach (FilterPicker p in panel.Controls)
            {
                if (null != p)
                {
                    p.BeginInvokeIfRequired(p.SetFields, fields);
                }
            }
        }
    }
}